package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.Result;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.entity.YddCate;
import com.itheima.ydd.service.YddArticleService;
import com.itheima.ydd.service.YddCateService;
import com.itheima.ydd.vo.YddArticleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/ydd_article")
public class YddArticleController {
    @Autowired
    private YddArticleService yddArticleService;

    @Autowired
    private YddCateService yddCateService;

    /**
     * 文章列表分页查询
     * * author:李多庆
     * @param start_time
     * @param end_time
     * @param title
     * @param cateid
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public Result list(Long start_time, Long end_time, String title, Integer cateid, Integer page, Integer limit) {
        //分页查询构造器
        Page<YddArticle> articlePage = new Page<>(page, limit);
        Page<YddArticleVo> articleVoPage = new Page<>();
        //构造条件查询器
        LambdaQueryWrapper<YddArticle> lqw = new LambdaQueryWrapper<>();
        lqw.like(title != null, YddArticle::getTitle, title);
        lqw.between(start_time != null && end_time != null, YddArticle::getCreate_date, start_time, end_time);
        lqw.eq(cateid != null, YddArticle::getCateid, cateid);
        Page<YddArticle> aPage = yddArticleService.page(articlePage, lqw);

        BeanUtils.copyProperties(articlePage, articleVoPage, "records");

        List<YddArticle> records = aPage.getRecords();
        List<YddArticleVo> list = records.stream().map(item -> {
            YddArticleVo yddArticleVo = new YddArticleVo();
            BeanUtils.copyProperties(item, yddArticleVo);
            Integer voCateid= item.getCateid();
            YddCate cate = yddCateService.getById(voCateid);
            String catename = cate.getCatename();
            yddArticleVo.setCatename(catename);
            return yddArticleVo;
        }).collect(Collectors.toList());
        articleVoPage.setRecords(list);

        PageResult answer = PageResult.answer(articleVoPage.getTotal(), articleVoPage.getSize(), articleVoPage.getCurrent(), articleVoPage.getRecords());
        return articlePage != null ? Result.ok("", answer) : Result.error("暂无标签数据");
    }

    /**
     * 文章删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public Result del(Integer id) {
        yddArticleService.removeById(id);
        return Result.ok("删除成功", null);
    }

    /**
     * 文章添加
     * 李多庆
     *
     * @param yddArticle
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody YddArticle yddArticle) {
        yddArticleService.save(yddArticle);
        return Result.ok("添加成功", null);

    }

    /**
     * 文章详情
     * 李多庆
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public Result add(Integer id) {
        YddArticle article = yddArticleService.getById(id);
        return Result.ok("", article);
    }

    /**
     * 文章修改
     * 李多庆
     * @param yddArticle
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody YddArticle yddArticle){
        yddArticleService.updateById(yddArticle);
        return Result.ok("修改成功",null);
    }

}
